package XMLSerialization;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.thoughtworks.xstream.XStream;

public class XMLClient {
	public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
		Socket serverSocket = new Socket("localhost", 90);
		XStream xstream = new XStream();
    	InputStream in = serverSocket.getInputStream();
    	OutputStream out = serverSocket.getOutputStream();
    	long time = System.currentTimeMillis();
    	for (int i = 0; i < 10000; i++) {
    		xstream.toXML(SensorData.generate(), out);
    		String end = (String)xstream.fromXML(in);
    		while (!(end.equals("end"))) {
    			Thread.sleep(5);
    			end = (String)xstream.fromXML(in);
    		}
    	}
    	System.out.println("throughput = "+1d * (System.currentTimeMillis() - time)/10000);
    	long timeDelay = System.currentTimeMillis();
    	xstream.toXML(SensorData.generate(), out);
    	String end = (String)xstream.fromXML(in);
		while (!(end.equals("end"))) {
			Thread.sleep(5);
			end = (String)xstream.fromXML(in);
		}
		System.out.println("delay = "+(System.currentTimeMillis() - timeDelay));
    	serverSocket.close();
	}
}
